var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
function isMailAddress(val) {
    if (val.match(emailfilter))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function isEmpty(val) {
    if (val.match(/^s+$/) || val == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function onCheckYahooid(yahooid){
    if(yahooid!=""){
            $("#alertYahooid").text("");
            return true;
        }else{
            $("#alertYahooid").text("(*)");
            return false;
        }
}

function onCheckPassword(password){
    if(password!=""){
        $("#alertPassword").text("");
        return true;
    }else{
        $("#alertPassword").text("(*)");
        return false;
    }
}

function onCheckUsername(username){
    if(username!=""){
        $("#alertUsername").text("");
        return true;
    }else{
        $("#alertUsername").text("(*)");
        return false;
    }
}


function onCheckMail(email){
    if(email!=""){
        if(isMailAddress(email)){
            $("#alertEmail").text("");
            return true;
        }else{
            $("#alertEmail").text("(Wrong)");
            return false;
        }
    }
    else{
        $("#alertEmail").text("(*)");
        return false;
    }
}

function checkInfoInsert(){
    var email = $("[name='inEmail']").val();
    var username = $("[name='inUsername']").val();
    var password = $("[name='inPassword']").val();
    var yahooid = $("[name='inYahooid']").val();
    onCheckMail(email);
    onCheckUsername(username);
    onCheckPassword(password);
    onCheckYahooid(yahooid);
    if(onCheckMail(email)&& onCheckUsername(username) && onCheckPassword(password) && onCheckYahooid(yahooid)){
        $("#btnsubmit").html("<input type=\"submit\" name =\"inSubmit\" value=\"Thêm Tài Khoản\">");
    }else{
        $("#btnsubmit").html("");
    }
}

function checkInfoUpdate(){
    var email = $("[name='upEmail']").val();
    var password = $("[name='upPassword']").val();
    var yahooid = $("[name='upYahooid']").val();
    onCheckMail(email);
    onCheckPassword(password);
    onCheckYahooid(yahooid);
    if(onCheckMail(email) && onCheckYahooid(yahooid)){
        $("#btnsubmit").html("<input type=\"submit\" name =\"inSubmit\" value=\"Sửa Tài Khoản\">");
    }else{
        $("#btnsubmit").html("");
    }
}

function DeleteAccount(){
    $("a.deleteAcc").click(function(){
        if(confirm("Bạn có chắc muốn xoá Tài Khoản:"+ $(this).attr("code") + "?") == true){
            $("div#AccountContent div.loading").css("width", $("div#AccountContent").width());
            $("div#AccountContent div.loading").css("height", $("div#AccountContent").height());
            $("div#AccountContent div.loading").css("display", "block");
            var temp = $(this).parent().parent();
            $.ajax({
                url: "../ajax/ajax.DeleteAccount.php?",
                type: "POST",
                data: {
                    "code" : $(this).attr("code")
                },
                cache: false,
                dataType: "html",
                success: function (data) {
                    if(data=="1")
                    {
                        temp.fadeOut('slow');
                    }
                    else
                    {
                        alert("Xảy ra lỗi khi xóa Tài Khoản");
                    }
                    $("div#AccountContent div.loading").css("display", "none");
                }
            });

        }
    });
}